//
//  TDSdSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/13.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


class TDSdSetViewController: NSViewController {
    var list:[String] = ["分辨率","帧率","码率"]
    var uCusViews:[CuSetView] = []
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: 400, height: 200))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255).cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        self.view.window?.styleMask = [.titled,.closable,.miniaturizable]
        self.view.window?.title = "共享设置"
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        let string1 = Td.sh().sd.param.videoResolution
        self.videoResolutionBtn.title = string1
        
        let string2 = "\(Td.sh().sd.param.videoFps)"+"fps"
        self.fpsBtn.title = string2
        
        self.systomAudio.state = Td.sh().sd.param.hasSystomAudio ? .on:.off
        
    }
    
    func setupUI() {
        self.view.addSubview(contentBg)
        contentBg.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(40, 20, 20, 20))
        }
        
        //初始化子类
        self.uCusViews = self.initCusViews(inView: self.contentBg, titles: self.list)
    }
    
    lazy var contentBg: NSView = {
        let view = NSView.init()
        return view
    }()
    
    lazy var videoResolutionBtn: NSPopUpButton = {
        let btn = NSPopUpButton.init(title: "640x360", target: self, action: #selector(videoResolutionAction(sender:)))
        btn.addItems(withTitles: ["640x360","640x480","960x540","1280x720"])
        return btn
    }()
    
    lazy var fpsBtn: NSPopUpButton = {
        let btn = NSPopUpButton.init(title: "15fps", target: self, action: #selector(fpsBtnAction(sender:)))
        btn.addItems(withTitles: ["15fps","20fps","25fps"])
        return btn
    }()
    
    lazy var systomAudio: NSButton = {
        let btn = NSButton.init()
        btn.title = "播放系统声音"
        btn.font = NSFont.systemFont(ofSize: 14)
        btn.setButtonType(NSButton.ButtonType.switch)
        btn.state = .off
        btn.target = self
        btn.action = #selector(systomAudioAction(sender:))
        return btn
    }()
    
    lazy var slide: VideoSlide = {
        let view = VideoSlide.init(value: Double(Td.sh().sd.param.videoBitrate), minValue: 500, maxValue: 2000) { (value) in
            self.slideAction(value:value)
        }
        return view
    }()
    
    func initCusViews(inView:NSView,titles:[String]) -> [CuSetView] {
        var uCusViews:[CuSetView] = []
        for (inx,val) in titles.enumerated() {
            var cusV:NSView?
            let cusSize:CGSize = CGSize.init(width: 200, height: 26)
            switch inx {
            case 0:
                cusV = self.videoResolutionBtn
            case 1:
                cusV = self.fpsBtn
            case 2:
                cusV = self.slide
            default:
                break
            }
            
            let cusView = CuSetView.init(text: val+" ", cusView: cusV, cusSize: cusSize)
            cusView.titleWidth = 220
            inView.addSubview(cusView)
            uCusViews.append(cusView)
            cusView.snp.makeConstraints { (make) in
                make.width.equalTo(cusSize.width+cusView.titleWidth+30)
                make.height.equalTo(cusSize.height)
                make.centerX.equalToSuperview().offset(-80)
                if inx == 0 {
                    make.top.equalTo(0)
                }else {
                    make.top.equalTo(uCusViews[inx-1].snp.bottom).offset(15)
                }
            }
        }
        return uCusViews
    }
}

extension TDSdSetViewController {
    @objc func systomAudioAction(sender:NSButton) {
        Td.sh().sd.param.hasSystomAudio = sender.state == .on ? true:false
    }
    
    @objc func fpsBtnAction(sender:NSPopUpButton) {
        let index = sender.indexOfSelectedItem
        Td.sh().sd.param.videoFps = index*5+15
    }
    
    @objc func videoResolutionAction(sender:NSPopUpButton) {
        Td.sh().sd.param.videoResolution = sender.title
    }
    
    func slideAction(value:Int) {
        Td.sh().sd.param.videoBitrate = value
    }
}
